package com.czh.www.domain;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DataPacket {
    private String version;
    private int commandNo;
    private int packetNo;
    private String senderName = null;
    private String senderHost = null;
    private String additional = null;
    private String ip = null;

    public DataPacket() {
    }

    public DataPacket(int commandNo) {
        this.commandNo = commandNo;
        this.packetNo = (int) (new Date()).getTime();
        this.version = "1";
        this.senderName = SystemVar.USER_NAME;
        this.senderHost = SystemVar.HOST_NAME;
    }

    public String getAdditional() {
        return this.additional;
    }

    public void setAdditional(String additional) {
        this.additional = additional;
    }

    public int getCommandNo() {
        return this.commandNo;
    }

    public void setCommandNo(int commandNo) {
        this.commandNo = commandNo;
    }

    public String getIp() {
        return this.ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public int getPacketNo() {
        return this.packetNo;
    }

    public void setPacketNo(int packetNo) {
        this.packetNo = packetNo;
    }

    public String getSenderHost() {
        return this.senderHost;
    }

    public void setSenderHost(String senderHost) {
        this.senderHost = senderHost;
    }

    public String getSenderName() {
        return this.senderName;
    }

    public void setSenderName(String senderName) {
        this.senderName = senderName;
    }

    public String getVersion() {
        return this.version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public int getCommandFunction() {
        return this.commandNo & 255;
    }

    public int getOption() {
        return this.commandNo & -256;
    }

    public static DataPacket createDataPacket(byte[] data, String ip) {
        if (data != null && ip != null) {
            try {
                String e = new String(data, SystemVar.DEFAULT_CHARACT);
                String[] dataArr = e.split(":");
                if (dataArr == null) {
                    return null;
                } else {
                    DataPacket packet = new DataPacket();
                    packet.setVersion(dataArr.length >= 1 ? dataArr[0] : "");
                    packet.setPacketNo(dataArr.length >= 2 ? Integer.parseInt(dataArr[1]) : -1);
                    packet.setSenderName(dataArr.length >= 3 ? dataArr[2] : "");
                    packet.setSenderHost(dataArr.length >= 4 ? dataArr[3] : "");
                    packet.setCommandNo(dataArr.length >= 5 ? Integer.parseInt(dataArr[4]) : 0);
                    if (dataArr.length >= 6) {
                        StringBuffer strBuf = new StringBuffer();

                        for (int i = 5; i < dataArr.length; ++i) {
                            strBuf.append(dataArr[i]);
                        }

                        packet.setAdditional(strBuf.toString());
                    }

                    packet.setIp(ip);
                    return packet;
                }
            } catch (UnsupportedEncodingException var7) {
                Logger.getLogger(DataPacket.class.getName()).log(Level.SEVERE, (String) null, var7);
                return null;
            } catch (Exception var8) {
                Logger.getLogger(DataPacket.class.getName()).log(Level.SEVERE, (String) null, var8);
                return null;
            }
        } else {
            return null;
        }
    }

    public String toString() {
        return this.version + ":" + this.packetNo + ":" + this.senderName + ":" + this.senderHost + ":" + this.commandNo + ":" + this.additional;
    }
}
